package com.nexgo.payment.trans.lock;

import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;

import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.EditDialogFragment;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.Iterator;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 终端锁定界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月22日
 */
public class LockTerminalActivity extends BaseActivity implements LockTerminalContract.View {
    private static final String TAG = LockTerminalActivity.class.getSimpleName();

    private EditDialogFragment editDialogFragment;
    private LockTerminalContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_terminal);
        ButterKnife.bind(this);

        getWindow().addFlags(3);  //屏蔽home键
        getWindow().addFlags(5);  //屏蔽菜单键

        initTitle();

        new LockTerminalPresenter(this, Repository.getInstance());
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setTitleText(getString(R.string.terminal_lock_title))
                .build();
    }

    @OnClick(R.id.btn_confirm)
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()){
            case R.id.btn_confirm:
                mPresenter.getInputDialog();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN){
            //拦截返回键，禁用返回按钮
            if (keyCode == KeyEvent.KEYCODE_BACK) return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setPresenter(LockTerminalContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showInputDialog() {
        editDialogFragment = new EditDialogFragment();
        editDialogFragment.setDialogType(EditDialogFragment.DIALOG_TYPE_WARNING);
        editDialogFragment.addTitle(getResources().getString(R.string.pls_input_security_password));
        editDialogFragment.addEditText1(InputType.TYPE_CLASS_NUMBER, 8, null, true);
        editDialogFragment.setPositionText(getString(R.string.confirm));
        editDialogFragment.setNegativeText(getString(R.string.cancel));
        editDialogFragment.setDialogListener(new EditDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
                editDialogFragment.dismiss();
            }

            @Override
            public void onConfirm(List<String> values) {
                Iterator<String> iterator = values.iterator();
                mPresenter.login(iterator.next());
            }
        });
        editDialogFragment.show(getSupportFragmentManager(),null);
    }

    @Override
    public void showEmptyInput() {
        ToastUtils.showToast(getString(R.string.input_can_no_be_empty));
    }

    @Override
    public void showErrPassword() {
        ToastUtils.showToast(getString(R.string.security_password_error));
    }

    @Override
    public void showExistLock() {
        if (editDialogFragment != null && editDialogFragment.getDialog() != null){
            Dialog dialog = editDialogFragment.getDialog();
            if (dialog.isShowing()) editDialogFragment.dismiss();
        }
        finish();
    }
}
